package com.pigs.pigsordersystem.controller;


import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pigs.pigsordersystem.entity.ResultFormat;
import com.pigs.pigsordersystem.entity.ResultFormatPaging;
import com.pigs.pigsordersystem.entity.UsersEntity;
import com.pigs.pigsordersystem.mapper.UsersMapper;
import com.pigs.pigsordersystem.service.IUsersService;
import com.pigs.pigsordersystem.util.ResultPagingUtil;

import com.pigs.pigsordersystem.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Y.yang
 * @since 2020-03-14
 */
@RestController
@RequestMapping("/users")
@Api(value = "/admin",description = "客户表控制器")
public class UsersController {

    @Autowired
    private UsersEntity usersEntity;

    @Autowired
    private IUsersService usersService;

    @Autowired
    private UsersMapper usersMapper;

    private AbstractWrapper wrapper = new QueryWrapper<UsersEntity>();

    private Logger logger = LoggerFactory.getLogger(UsersController.class);

    /**
     * 查询全部用户信息 并分页
     *
     * @return
     */
    @GetMapping("queryUserAll")
    @ApiOperation(value = "查询全部用户信息 并分页")
    public ResultFormatPaging queryUserAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {

        Page<UsersEntity> iPage = new Page<UsersEntity>(page,limit);

        IPage<UsersEntity> moveBookingEntityIPage = usersService.queryUserList(iPage, wrapper);

        List<UsersEntity> userList = iPage.getRecords();
        logger.info("userList={}", userList);

        return ResultPagingUtil.pagingSuccess(0, (int) iPage.getTotal(), userList);
    }

    /**
     * 客户登录
     * @param name
     * @param userPassword
     * @param session
     * @return
     */
    @PostMapping("userLogin")
    @ApiOperation(value = "客户登录")
    public ResultFormat loginAdmin(String name, String userPassword, HttpSession session) {
        UsersEntity usersEntity = usersService.loginUser(name, userPassword);
        logger.info("usersEntity ={}",usersEntity );
        if (usersEntity!=null){
            session.setAttribute("userName",name);
            return ResultUtil.success();
        }

        return ResultUtil.error(100,"密码或账号输出错误");
    }


    /**
     * 管理员退出登录
     * exit
     * @return
     */
    @GetMapping("exitLogin")
    public ResultFormat exitLogin(HttpSession session){
        session.removeAttribute("userName");
        return ResultUtil.success("退出成功");
    }


}
